package cn.iocoder.yudao.module.bpm.message.handle;

import cn.iocoder.yudao.module.bpm.message.dto.BpmProcessTaskAssignedMessage;
import cn.iocoder.yudao.module.bpm.message.dto.BpmProcessTaskCancelledMessage;
import cn.iocoder.yudao.module.bpm.message.dto.BpmProcessTaskCompletedMessage;
import cn.iocoder.yudao.module.bpm.message.dto.BpmProcessTaskCreatedMessage;
import cn.iocoder.yudao.module.system.api.notify.NotifyMessageSendApi;
import cn.iocoder.yudao.module.system.api.notify.dto.NotifySendSingleToUserReqDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;


@Slf4j
@RequiredArgsConstructor
@Component
public class ProcessTaskMessageHandleImpl implements ProcessTaskMessageHandle{


    @Resource
    private NotifyMessageSendApi notifyMessageSendApi;

    static final String BPM_TASK_ASSIGNED_MSG = "BPM_TASK_ASSIGNED_MSG";

    public void onProcessInstanceTaskCreatedMsg(BpmProcessTaskCreatedMessage bpmProcessTaskCreatedMessage){
        log.info("任务创建消息：{}", bpmProcessTaskCreatedMessage);
    }
    public void onProcessInstanceTaskAssignedMsg(BpmProcessTaskAssignedMessage bpmProcessTaskAssignedMessage){
        log.info("任务分配消息：{}", bpmProcessTaskAssignedMessage);

        try {
            HashMap<String, Object> templateParams = new HashMap<>();

            templateParams.put("processName", bpmProcessTaskAssignedMessage.getProcessName());
            templateParams.put("processId", bpmProcessTaskAssignedMessage.getProcessId());

            NotifySendSingleToUserReqDTO notifySendSingleToUserReqDTO = new NotifySendSingleToUserReqDTO()
                    .setUserId(Long.valueOf(bpmProcessTaskAssignedMessage.getAssignee()))
                    .setTemplateCode(BPM_TASK_ASSIGNED_MSG)
                    .setTemplateParams(templateParams);
            notifyMessageSendApi.sendSingleMessageToAdmin(
                    notifySendSingleToUserReqDTO
            );

        } catch (Exception e) {
            log.error("MQ消费失败",e);
            throw new RuntimeException(e.getMessage());
        }

    }
    public void onProcessInstanceTaskCleanMsg(BpmProcessTaskCancelledMessage bpmProcessTaskCancelledMessage){
        log.info("任务取消消息：{}", bpmProcessTaskCancelledMessage);
    }
    public void onProcessInstanceTaskFinishedMsg(BpmProcessTaskCompletedMessage bpmProcessTaskCompletedMessage){
        log.info("任务完成消息：{}", bpmProcessTaskCompletedMessage);
    }


}
